#include "ringQueue.hpp"
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>

void *consumer(void *args)
{
    RingQueue<int> *prq = (RingQueue<int> *)args;

    int x = 0;
    while(true)
    {
        sleep(1);
        
        prq->Pop(&x);

        std::cout << pthread_self() << "  消费：" << x << std::endl;
    }
}

void *productor(void *args)
{
    RingQueue<int> *prq = (RingQueue<int> *)args;
    int x = 0;
    while(true)
    {
        x = rand()%100+1;
        prq->Push(x);
        std::cout << pthread_self() << "  生产: " << x << std::endl;
        //sleep(1);
    }
}

int main()
{
    srand((uint64_t)time(nullptr) ^ getpid());
    RingQueue<int> rq;
    //rq.debug();

    // pthread_t c, p;
    // pthread_create(&c, nullptr, consumer, (void *)&rq);
    // pthread_create(&p, nullptr, productor, (void *)&rq);

    // pthread_join(c, nullptr);
    // pthread_join(p, nullptr);

    pthread_t c[3], p[2];
    pthread_create(c, nullptr, consumer, (void *)&rq);
    pthread_create(c+1, nullptr, consumer, (void *)&rq);
    pthread_create(c+2, nullptr, consumer, (void *)&rq);
    pthread_create(p, nullptr, productor, (void *)&rq);
    pthread_create(p+1, nullptr, productor, (void *)&rq);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);
    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    return 0;
}